package com.baizhi.config;

import com.baizhi.filter.AccessControlAllowFilter;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.net.InetAddress;

@Configuration//该类是一个配置类，替换spring的配置文件
public class MyConfig implements WebMvcConfigurer {
    @Bean
    public FilterRegistrationBean filterRegist() {
        //管理过滤器的一个管理类，我们把自己的过滤交个这个类进行注册管理
        FilterRegistrationBean frBean = new FilterRegistrationBean();
        frBean.setFilter(new AccessControlAllowFilter());
        frBean.addUrlPatterns("/*");//过滤都有一个url-pattern
        return frBean;
    }

    @Bean
    public TransportClient getClient(){
        try {
            TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.107.131"), 9300));
            return transportClient;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
